package com.wsj.a.collapsingdemo.utils;

import android.util.Log;

/**
 * Author：程必威
 * Email: 137880411@qq.com
 * data: 2018/1/15
 * TODO：打印工具类
 */

public class Logger {
    /**
     * whether print log info
     */
    public static boolean weatherPrint = true;
//    public static boolean weatherPrint = false;//打包设置成 false
    static String className;//类名
    static String methodName;//方法名
    static int lineNumber;//行数

    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }

    private static String createLog(String log) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(methodName);
        buffer.append("(").append(className).append(":").append(lineNumber).append(")");
        buffer.append(log);
        return buffer.toString();
    }

    public static void d(String msg) {
        if (!weatherPrint)
            return;

        getMethodNames(new Throwable().getStackTrace());
        if (msg.length() > 3000) {
            for (int i = 0; i < msg.length(); i += 3000) {
                if (i + 3000 < msg.length()) {
                    Log.i(className + " " + i, createLog(msg.substring(i, i + 3000)));
                } else {
                    Log.i(className + " " + i, createLog(msg.substring(i, msg.length())));
                }
            }
        } else
            Log.i(className, createLog(msg));
    }

    public static void v(String msg) {
        if (!weatherPrint)
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, createLog(msg));
    }

    public static void i(String msg) {

        if (!weatherPrint)
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, createLog(msg));
    }

    public static void w(String msg) {

        if (!weatherPrint)
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, createLog(msg));
    }

    public static void e(String msg) {

        if (!weatherPrint)
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, createLog(msg));
    }

    public static boolean isLoggable(int level) {
        //return Log.isLoggable(tag.substring(0, Math.min(23, tag.length())), level);
        return true;
    }
}
